home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / GUSI 1.4.1 / GUSI / GUSIRemoteConsole.cp < prev    next >
Encoding:
Text File  |  1994-02-25  |  2.8 KB  |  115 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    GUSI                        -    Grand Unified Socket Interface
  3. File        :    GUSIRemoteConsole.cp    -    Divert standard I/O to other 
  4.                                                 application.
  5. Author    :    Matthias Neeracher
  6. Language    :    MPW C++
  7.  
  8. $Log: GUSIRemoteConsole.cp,v $
  9. Revision 1.1  1994/02/25  02:30:15  neeri
  10. Initial revision
  11.  
  12. *********************************************************************/
  13.  
  14. #pragma force_active on
  15.  
  16. #include "GUSIPPC_P.h"
  17.  
  18. #include <SysEqu.h>
  19. #include <Script.h>
  20. #include <TextUtils.h>
  21. #include <PLStringFuncs.h>
  22.  
  23. class GUSIRemoteConsoleDomain : public DeviceSocketDomain {
  24.     static Socket *    console;
  25. public:
  26.     GUSIRemoteConsoleDomain()    :    DeviceSocketDomain(AF_REMOTECON)    {    }
  27.     
  28.     virtual     Socket * open(const char * filename, int oflag);
  29. };
  30.  
  31. GUSIRemoteConsoleDomain    GUSIRemoteConsoleSockets;
  32.  
  33. Socket * GUSIRemoteConsoleDomain::console    =    (Socket *) nil;
  34.  
  35. static sa_constr_ppc    RemoteConConstr    =    {
  36.     PPC_CON_NEWSTYLE+PPC_CON_MATCH_TYPE,
  37.     "\p",
  38.     {
  39.         smRoman,
  40.         "\p",
  41.         ppcByString,
  42.         "\pRemote Console"
  43.     }
  44. };
  45.  
  46. Socket * GUSIRemoteConsoleDomain::open(const char * filename, int flags)
  47. {
  48.     char                 title[256];
  49.     
  50.     strncpy(title, filename, 7);
  51.     title[7] = 0;
  52.     
  53.     if (equalstring(title, (char *) "stdin", false, true)) {
  54.         flags = O_RDONLY;
  55.         
  56.         if (filename[5])
  57.             return (Socket *) TryNextDevice;
  58.         else
  59.             filename += 5;
  60.     } else if (equalstring(title, (char *) "stdout", false, true)) {
  61.         flags = O_WRONLY;
  62.         
  63.         if (filename[6])
  64.             return (Socket *) TryNextDevice;
  65.         else
  66.             filename += 6;
  67.     } else if (equalstring(title, (char *) "console", false, true)) {
  68.         if (filename[7])
  69.             return (Socket *) TryNextDevice;
  70.         else
  71.             filename += 7;
  72.     } else
  73.         return (Socket *) TryNextDevice;
  74.  
  75.     if (!console) {
  76.         if (!(console = PPCSockets.socket(SOCK_STREAM, 0)))
  77.             goto lose;
  78.         
  79.         sockaddr_ppc    addr;
  80.         int                len = sizeof(sockaddr_ppc);
  81.     
  82.         addr.family                                    =    AF_PPC;
  83.         addr.port.nameScript                        =    smRoman;
  84.         addr.port.portKindSelector                =    ppcByString;
  85.         addr.location.locationKindSelector    =    ppcNBPTypeLocation;
  86.         
  87.         PLstrcpy(addr.port.name, (StringPtr) CurApName);
  88.         PLstrcpy(addr.location.u.nbpType, "\pPPCToolBox");
  89.         PLstrcpy(addr.port.u.portTypeStr, "\pRemote Console Client");
  90.         
  91.         if (console->bind((struct sockaddr *) &addr, sizeof(sockaddr_ppc))) {
  92.             if (errno != EADDRINUSE)
  93.                 goto lose;
  94.                 
  95.             strcpy((char *) addr.port.name+addr.port.name[0]+1, " - 2");
  96.             addr.port.name[0] += 4;
  97.             
  98.             while (console->bind((struct sockaddr *) &addr, sizeof(sockaddr_ppc))) {
  99.                 if (errno != EADDRINUSE || addr.port.name[addr.port.name[0]] == '9')
  100.                     goto lose;
  101.                 ++addr.port.name[addr.port.name[0]];
  102.             }
  103.         }
  104.         if (PPCSockets.choose(0, "Please choose a console to connect to", &RemoteConConstr, 0, &addr, &len))
  105.             goto lose;
  106.         if (console->connect(&addr, len))
  107.             goto lose;
  108.     }
  109.     
  110.     return console;
  111.  
  112. lose:
  113.     exit(1);
  114. }
  115.